import os
import pandas as pd
import logging


class Variables:
    """
    项目中使用到的变量
    """
    # global variables
    WORK_PATH = os.path.split(os.path.abspath(__file__))[0]


class RemainTimePredParameters:
    def __init__(self, dataset_name):
        if dataset_name == "bpic2013":
            self._RAW_DATA_PATH = "/data/raw_data/BPIC2013/12693914/BPI_Challenge_2013_incidents.xes"
            self._TRAIN_DATA_PATH = "/data/filtered_data/BPIC 2013/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/BPIC 2013/test_data.npy"
            self._RESULT_PATH = "/results/rtp_results/bpic2013"

        # 该数据集被BPIC 2015代替
        if dataset_name == "environmental_permit":
            self._RAW_DATA_PATH = "/data/raw_data/EnvironmentalPermit/12714599/CoSeLoG WABO 1.xes"
            self._TRAIN_DATA_PATH = "/data/filtered_data/Environmental Permit/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/Environmental Permit/test_data.npy"
            self._RESULT_PATH = "/results/environmental_permit"

        if dataset_name == "hospital_billing":
            self._RAW_DATA_PATH = "/data/filtered_data/Hospital Billing/filtered_data.csv"
            self._TRAIN_DATA_PATH = "/data/filtered_data/Hospital Billing/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/Hospital Billing/test_data.npy"
            self._RESULT_PATH = "/results/rtp_results/hospital_billing"

        if dataset_name == "bpic2015":
            self._RAW_DATA_PATH = "/data/raw_data/BPIC2015/12709154/BPIC15_1.xes"
            self._TRAIN_DATA_PATH = "/data/filtered_data/BPIC 2015/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/BPIC 2015/test_data.npy"
            self._RESULT_PATH = "/results/bpic2015"

        if dataset_name == "bpic2012":
            self._RAW_DATA_PATH = "/data/filtered_data/BPIC2012/filtered_data.csv"
            self._TRAIN_DATA_PATH = "/data/filtered_data/BPIC2012/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/BPIC2012/test_data.npy"
            self._RESULT_PATH = "/results/rtp_results/bpic2012"

        if dataset_name == "bpic2018":
            self._RAW_DATA_PATH = "/data/filtered_data/BPIC2018/filtered_data_v1.csv"
            self._TRAIN_DATA_PATH = "/data/filtered_data/BPIC2018/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/BPIC2018/test_data.npy"
            self._RESULT_PATH = "/results/rtp_results/bpic2018"

        if dataset_name == "bpic2019":
            self._RAW_DATA_PATH = "/data/filtered_data/BPIC2019/filtered_data.csv"
            self._TRAIN_DATA_PATH = "/data/filtered_data/BPIC2019/train_data.npy"
            self._TEST_DATA_PATH = "/data/filtered_data/BPIC2019/test_data.npy"
            self._RESULT_PATH = "/results/rtp_results/bpic2019"

        self.RAW_DATA_PATH = Variables.WORK_PATH + self._RAW_DATA_PATH
        self.TRAIN_DATA_PATH = Variables.WORK_PATH + self._TRAIN_DATA_PATH
        self.TEST_DATA_PATH = Variables.WORK_PATH + self._TEST_DATA_PATH
        self.RESULT_PATH = Variables.WORK_PATH + self._RESULT_PATH


class DRLParameters:
    """
    深度强化学习的参数
    """

    def __init__(self, dataset_name):
        self._RESULT_PATH = "/results/drl_results/" + dataset_name  # 结果保存路径
        self._DATA_PATH = "/data/drl_data/" + dataset_name  # 数据集路径
        self._CSV_PATH = self._DATA_PATH + "/" + dataset_name + "_nn_sample250.csv"  # CSV文件路径
        self.DATA_PATH = Variables.WORK_PATH + self._DATA_PATH  # 数据集绝对路径
        self.TEST_CASE_ID_PATH = None

        self.EPSILON = 0.9
        self.GAMMA = 0.9

        # BPIC 2013数据集相关参数
        if dataset_name == "bpic2013":
            self.INITIAL_CASE_IDS = [732139419, 731984877]  # 初始时case id
            self.DEBUG = False
            self.ACTION_SPACE = 10  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 10  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 16  # 神经元个数
            self.REPLAY_SIZE = 1000  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 512
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

            self.TEST_CASE_ID_PATH = self.DATA_PATH + "/test_case_ids_v1.npy"  # 测试集case id路径

        if dataset_name == "environmental_permit":
            self.INITIAL_CASE_IDS = [6260081, 4320987]  # 初始时case id
            self.DEBUG = True

        if dataset_name == "bpic2015":
            self.INITIAL_CASE_IDS = [2832460]
            self.DEBUG = False
            self.ACTION_SPACE = 18  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 10  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 16  # 神经元个数
            self.REPLAY_SIZE = 1000  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 32
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

        # Hospital Billing数据集相关参数
        if dataset_name == "hospital_billing":
            self.INITIAL_CASE_IDS = [1387420000]
            self.DEBUG = False
            self.ACTION_SPACE = 8  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 30  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 16  # 神经元个数
            self.REPLAY_SIZE = 500  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 1024
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

        if dataset_name == "bpic2012":
            self.INITIAL_CASE_IDS = [197005, 175624]
            self.DEBUG = False
            self.ACTION_SPACE = 15  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 10  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 16  # 神经元个数
            self.REPLAY_SIZE = 2000  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 256
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

        if dataset_name == "bpic2018":
            self.INITIAL_CASE_IDS = [-1357128597]
            self.DEBUG = False
            self.ACTION_SPACE = 30  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 10  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 32  # 神经元个数
            self.REPLAY_SIZE = 2000  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 128
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

            self.TEST_CASE_ID_PATH = self.DATA_PATH + "/test_case_ids_v1.npy"  # 测试集case id路径

        if dataset_name == "bpic2019":
            self.INITIAL_CASE_IDS = [-695009253]
            self.DEBUG = False
            self.ACTION_SPACE = 13  # 动作空间取值 0, 1, ..., ACTION_SPACE
            self.WINDOW_SIZE = 10  # 历史活动的窗口大小
            self.TARGET_REPLACE_ITER = 10
            self.N_NEURONS = 16  # 神经元个数
            self.REPLAY_SIZE = 2000  # 记忆回放大小
            self.LEARNING_RATE = 0.003  # 学习率
            self.BATCH_SIZE = 64
            self.N_LAYERS = 1  # LSTM层数
            self.N_EPISODES = 2500  # 迭代次数

        self.RESULT_PATH = Variables.WORK_PATH + self._RESULT_PATH  # 结果绝对路径
        self.TRAIN_CASE_ID_PATH = self.DATA_PATH + "/train_case_ids.npy"  # 训练集case id路径
        if self.TEST_CASE_ID_PATH is None:
            self.TEST_CASE_ID_PATH = self.DATA_PATH + "/test_case_ids.npy"  # 测试集case id路径
        self.ACTIVITY_ERRORS_PATH = self.DATA_PATH + "/activity_errors.json"

        # 读取dataframe
        try:
            logging.info("read sample file into data frame.")
            self._Data_Frame = pd.read_csv(Variables.WORK_PATH + self._CSV_PATH)
        except Exception as e:
            logging.error("%s: read sample file failed." % e)
            self._Data_Frame = None

    def get_data_frame(self):
        """
        返回data frame文件
        :return:
        """
        return self._Data_Frame
